/**
 * Created by seeker910 on 2017/7/11.
 *
 * @description
 */
Rsd.define('Rsd.control.SearchToolBar', {
    extend: 'Rsd.container.Component',
    requires:[
        'Rsd.form.Button',
        'Rsd.form.Text',
        'Rsd.form.ComboBox'
    ],
    xtype:'search-tool-bar',
    bodyCls:'x-grid-tool-bar',
    bodyStyle:{minHeight:'unset'}, //display:'flex', //添加display:'flex' 会导致元素style.float无效
    height: '50px',
    header:{visible:false}, 
    layout:{type:'hbox',align:'left'},
    items:[],
    
    /**
    * */
    constructor: function GridToolBar(config) {
        this.header.visible = false;
        config = config || {};
        this.apply(config); 
    },
    //  /**
    //   * 
    //   */
    // onAfterRender:function onAfterRender()
    // {
    //     this.callParent();
        
    //     console.log('toolbar before render',this.newButtons.length,this.searchColumns.length);
    // },
    /**
     *@description 设置查询字段
     * */
    setSearchColumns:function () {

        var me = this;
        var columns = me.searchColumns||[]; 
        if(Rsd.isArray(columns))
        {
            setTimeout(function () {
                me.body.element.style.visibility = 'hidden'; 
                for(var i in columns)
                {
                    var _c = columns[i]; 
                    _c.xtype = _c.xtype ||'text';
                    if(_c.label !== false && _c.label !== null)
                    {
                        if(Rsd.isEmpty(_c.label))
                        {
                            _c.label = {content:_c.text||'',height:30}; 
                        }
                        if(Rsd.isString(_c.label))
                        {
                            _c.label = {content:_c.label,height:30}; 
                        } 
                    }
                    
                    _c.dataIndex = _c.dataIndex||_c.name;
                    _c.name = _c.name || _c.dataIndex ;
                    _c.height= _c.height||30;
                    _c.margin = _c.margin || "0 0 0 20";
                   
                    me.insertItem(i,_c); 
                }
                setTimeout(function () {
                    me.body.element.style.visibility = 'visible';
                },50);

            },0);
        }

    },

    /**
     * @description 设置添加到按钮
     * */
    setNewButtons:function () {

        var me = this;
        var _buttons = this.newButtons||[];  
        if(Rsd.isArray(_buttons))
        {
            setTimeout(() => {
            
                for (var i in _buttons) {
                    var _btn = _buttons[i];
                    var _item = {
                        xtype: 'button',
                        text:  '---',
                        align: 'left',
                        style: {display: _btn.hasOwnProperty('visible')?(_btn.visible ? 'display' : 'none'):'display'},
                        margin:'2 10 2 10',
                        height:35,
                        width: 120,
                        index: i,
                        handler: function () {
                            me.funApplyByIOC(_buttons[this.index].handler,arguments);
                        }
                    };
                    this.addItem(Rsd.apply(_item,_btn));
    
                }
            }, 10);
    
        }
       
    },
    /**
    * @param ignoreNone 忽略op=='none'的条件
    * */
    getWhere:function getWhere(ignoreNone){

        //debugger;
        var _c =[];
        var _default = this;
        for(var i in this.searchColumns){

            var _item = this.searchColumns[i];
            var _name = _item.name||_item.dataIndex||_item.dataindex||_item.id||_item.key;
            var _ctrl = _default.getItemByName(_name);
            if(_ctrl == null)
            {
                console.error('筛选条件，未找到'+_name+'控件');
                continue;
            }
            var _value = _ctrl.getValue();
            if ( Rsd.isEmpty(_value)) {
                continue;
            }
            if(Rsd.isArray(_value))
            {
                var _str = '';
                for(var i in _value)
                { 
                    _str += (_value[i]['value']+',')
                }
                _value = _str;
            }

            if(Rsd.isString(_value))
            {
                _value = _value.trim();
            }
            
            var _op =  _item.op;
            if(ignoreNone==true && _op == 'none')
            {
                continue;
            }
            if (Rsd.isEmpty(_item.op) ) {

                _op =  'like';
            }
            if(_value.lastIndexOf(',')>0)
            {
                _value = _value.split(',');
            }
            if( _op == 'like')
            {
                if(Rsd.isArray(_value))
                {
                    var _temp = [];
                    for(var i in _value)
                    {
                        var v = _value[i];
                        if(Rsd.isEmpty(v))
                        {
                            continue;
                        }
                        _temp.push('%25' + v.trim() + '%25');
                    }
                    _value = _temp;
                }
                else
                {
                    _value = '%25' + _value.trim() + '%25';
                }

            }

            if(_ctrl instanceof Rsd.form.CheckBox )
            {
                if(!_ctrl.isChecked())
                {
                    continue;
                }
            }


            var _dataIndex = _item.dataIndex || _item.name ||_item.id || _item.key;
            if (Rsd.isArray(_dataIndex))
            {
                for(var i in _dataIndex)
                {
                    if(Rsd.isEmpty(_dataIndex[i]))
                    {
                        continue;
                    }
                    _c.push({group:_item.group||_item.name||'defualt',name:_dataIndex[i],value:_value,op:_op,logic:(i==0)?'none':'or'});
                }
            }
            else
            {
                var _op_str = _op.replaceAll(' ','').toLowerCase();
                if ( _op_str == 'isnull' || _op_str == 'isnotnull' ) {

                    _c.push({name:_dataIndex,op:_op});
                }
                else
                {
                    _c.push({name:_dataIndex,value:_value + '',op:_op});
                }


            }
        } 

        return _c;

    }

},function (type) {
    //
    var _newButtonsGetter = function () {

        return this.__newButtons||[];
    };

    var _newButtonsSetter = function (value) {
        this.__newButtons = value;
        this.setNewButtons();

    }

    this.defineProperty(type,"newButtons", _newButtonsGetter, _newButtonsSetter,true);
    //
    var _searchColumnsGetter = function () {

        return this.__searchColumns||[];
    };

    var _searchColumnsSetter = function (value) {
        this.__searchColumns = value||[];  
        this.setSearchColumns();
    }

    this.defineProperty(type,"searchColumns", _searchColumnsGetter, _searchColumnsSetter,true);

});